package com.zhiwang.xiaoqu.widget

import android.app.AlertDialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.widget.TextView
import com.zhiwang.xiaoqu.R
import kotlinx.android.synthetic.main.update_alert_layout.view.*

class UpdateDialog {


    companion object UpdateDialogSoul {

        //更新进度
        fun create(context: Context, version: String): UpdateDialog {

            val dialogView = LayoutInflater.from(context).inflate(R.layout.update_progress_layout, null)

            val updateProgressBar = dialogView.findViewById<UpdateProgressBar>(R.id.updateProgressBar)

            dialogView.findViewById<TextView>(R.id.tv_latest_version).text = version

            val dialog = AlertDialog.Builder(context, R.style.LargeAlertDialogStyle)
                .setView(dialogView)
                .create()
            dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
            return UpdateDialog(dialog, updateProgressBar)
        }


        fun create(
            context: Context,
            version: String,
            isForceUpdate: Boolean,
            updateLog: String,
            positiveListener: View.OnClickListener?,
            negativeListener: View.OnClickListener?
        ): UpdateDialog {

            val dialogView = LayoutInflater.from(context).inflate(R.layout.update_alert_layout, null)

            dialogView.app_version.text = "V$version 版"
            dialogView.update_log.text = updateLog
            dialogView.upgrade_now.setOnClickListener(positiveListener)
            dialogView.not_now.setOnClickListener(negativeListener)

            val dialog = AlertDialog.Builder(context, R.style.LargeAlertDialogStyle)
                .setView(dialogView)
                .create()
            dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))

            return UpdateDialog(dialog = dialog, updateProgressBar = null)
        }
    }


    var updateProgressBar: UpdateProgressBar?
    var dialog: AlertDialog

    private constructor(dialog: AlertDialog, updateProgressBar: UpdateProgressBar?) {
        this.dialog = dialog
        this.updateProgressBar = updateProgressBar
    }


    fun setProgress(progress: Int) {
        this.updateProgressBar?.progress = progress
    }

    fun dismiss() {
        this.dialog.dismiss()
    }

    fun show() {
        this.dialog.show()
    }

    interface DialogInterface {

    }
}